package com.example.financialsystem.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.financialsystem.R;
import com.rengwuxian.materialedittext.MaterialEditText;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import others.ServerResponse;

import static others.Const.IP;
import static others.Const.PORT;
import static others.Const.currentBook;
import static others.Const.currentUserVo;
import static others.Tools.ToastShow;
import static utils.GsonUtil.Json2Class;

public class InviteMemberDialog extends Dialog implements View.OnClickListener {

    Activity context;

    private MaterialEditText editMemberName;
    private Button buttonSubmit;

    private String memberName;
    private String responseData;
    private final static int GET_RESPONSE = 1;

    public InviteMemberDialog(Activity context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_invite_member);

        Window dialogWindow = this.getWindow();
        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        /*p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6*/
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(p);
        this.setCancelable(true);

        editMemberName = (MaterialEditText)findViewById(R.id.invite_edit_memberName);
        buttonSubmit = (Button)findViewById(R.id.invite_button_submit);
        buttonSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.invite_button_submit:
                memberName = editMemberName.getText().toString();
                String url = "http://" + IP + ":" + PORT + "/portal/book/inviteMember.do";
                RequestBody requestBody = new FormBody.Builder()
                        .add("senderId",currentUserVo.getId())
                        .add("memberName",memberName)
                        .add("bookId",currentBook.getId())
                        .build();
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                sendRequest(request);
                break;
        }
    }

    public void sendRequest(Request request){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();
                    Response response = client.newCall(request).execute();
                    responseData = response.body().string();
                    Message message = new Message();
                    message.what = GET_RESPONSE;
                    handler.sendMessage(message);
                } catch (Exception e){
                    e.printStackTrace();
                    ToastShow("我们可能遭遇了一些麻烦1(InviteMemberDialog)");
                }
            }
        }).start();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case GET_RESPONSE:
                    ServerResponse serverResponse = Json2Class(responseData);
                    if (serverResponse.getStatus() == 0){
                        ToastShow("邀请通知已经发送给该用户");
                        dismiss();
                    }else {
                        ToastShow(serverResponse.getMsg()+"(InviteMemberDialog)");
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
